// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
    jialiao:[
      {name:'布蕾',flag:false,price:3,id:1},{name:'芋泥',flag:false,price:3,id:2},{name:'西米',flag:false,price:1,id:3},{name:'爱玉',flag:false,price:1,id:4},{name:'啵啵',flag:false,price:2,id:5},{name:'红豆',flag:false,price:2,id:6},{name:'珍珠',flag:false,price:1,id:7},{name:'燕麦',flag:false,price:2,id:8},{name:'芋圆',flag:false,price:1,id:9},{name:'椰果',flag:false,price:1,id:10},{name:'黑砖冻',flag:false,price:2,id:11},{name:'黄金芝士',flag:false,price:3,id:12},{name:'芝士',flag:false,price:3,id:13},{name:'斑斓糯米',flag:false,price:3,id:14},
    ],
    Tang:[{name:'正常糖',flag:false,id:1,fic:'sugar'},{name:'少糖',flag:false,id:2,fic:'sugar'}],
    Bing:[{name:'标准冰',flag:false,id:1,fic:'ice'},{name:'少冰',flag:false,id:2,fic:'ice'}],
    numFlag:0,
    TangNumFlag:0,
    BingNumFlag:0,
    Allprice:app.globalData.teaPrice,
    liaoprice:0,
    teaDescribe:app.globalData.teaDescribe,
    BuyNum:1,
    teaName:app.globalData.teaName,
    jialiaoList:'',
    Tanglist:'',
    Binglist:'',
    temp:'',
    showView:true,
    result:'',
    cartCount:0,
  },

  selectJiaLiao:function(e){
    // console.log(e);
    var jialiaolist = this.data.jialiaoList;
    const id = e.currentTarget.dataset.id
    const jialiao = this.data.jialiao;
    var n = this.data.numFlag;
    var allprice = this.data.Allprice;

    for (let i = 0, len = jialiao.length; i < len; ++i) {
      if (jialiao[i].id === id) {
        if(n<2 && jialiao[i].flag==false){
          jialiao[i].flag = !jialiao[i].flag
          this.setData({numFlag:n+1});
          this.setData({Allprice:allprice+jialiao[i].price})
          this.setData({liaoprice:this.data.liaoprice+jialiao[i].price})
          this.setData({temp:jialiaolist})
          this.setData({jialiaoList:jialiaolist+'+'+jialiao[i].name})
        }else if(jialiao[i].flag){
          jialiao[i].flag = false;
          this.setData({numFlag:n-1});
          this.setData({Allprice:allprice-jialiao[i].price})
          this.setData({liaoprice:this.data.liaoprice-jialiao[i].price})
          if(this.data.liaoprice == 0){
            this.setData({jialiaoList:''})
          }else{
            this.setData({jialiaoList:this.data.temp})
          }
        }
      }
    }
    
    this.setData({
      jialiao
    })
  },

  selectTangBing:function(e){
    var tanglist = this.data.Tanglist;
    var binglist = this.data.Binglist;
    const id = e.currentTarget.dataset.id
    const Tang = this.data.Tang;
    const Bing = this.data.Bing;
    var n = this.data.TangNumFlag;
    var m = this.data.BingNumFlag;

    if(e.currentTarget.dataset.fic == "sugar"){
      for (let i = 0, len = Tang.length; i < len; ++i) {
        if (Tang[i].id === id) {
          if(n<1){
            Tang[i].flag = !Tang[i].flag
            this.setData({TangNumFlag:n+1});
            this.setData({Tanglist:tanglist+'+'+Tang[i].name})
          }else if(Tang[i].flag){
            Tang[i].flag = false;
            this.setData({TangNumFlag:n-1});
            this.setData({Tanglist:''})
          }
        }    
      }
      this.setData({
        Tang
      })
    }else if(e.currentTarget.dataset.fic == "ice"){
      for (let i = 0, len = Bing.length; i < len; ++i) {
        if (Bing[i].id === id) {
          if(m<1){
            Bing[i].flag = !Bing[i].flag
            this.setData({BingNumFlag:m+1});
            this.setData({Binglist:binglist+'+'+Bing[i].name})
          }else if(Bing[i].flag){
            Bing[i].flag = false;
            this.setData({BingNumFlag:m-1});
            this.setData({Binglist:''})
          }
        }    
      }
      this.setData({
        Bing
      })
    }
  },

  surNumber:function(e){
    var n=this.data.BuyNum;
    if(this.data.BuyNum>1){
      this.setData({BuyNum:n-1});
    }
  },
  addNumber:function(e){
    var n=this.data.BuyNum;
    this.setData({BuyNum:n+1});
  },

  gotoCart:function(e){
    // this.setData({LastResult:{name:this.data.teaName,num:this.data.BuyNum,allPrice:this.data.Allprice*this.data.BuyNum}})
    app.globalData.LastResult.push({id:this.data.cartCount,name:this.data.teaName,num:this.data.BuyNum,allPrice:this.data.Allprice*this.data.BuyNum,jialiao:this.data.jialiaoList+this.data.Tanglist+this.data.Binglist})

    this.setData({
      showView: false,
      result:app.globalData.LastResult,
      cartCount:this.data.cartCount+1
    })
    // console.log(app.globalData.LastResult)
    // console.log(e)
  },

  close: function () {
    let that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },

  delCart:function(e){
    // console.log(e)
    var cartid = e.currentTarget.dataset.cartkey;
    app.globalData.LastResult.splice(cartid,1);
    this.setData({
      result:app.globalData.LastResult
    })
  },
  bindMinus:function(e){
    var cartid = e.currentTarget.dataset.minkey;
    var oneliaoprice = app.globalData.LastResult[cartid].allPrice / app.globalData.LastResult[cartid].num;
    // app.globalData.LastResult[cartid].num--;
    // console.log(app.globalData.LastResult[cartid].num)
    if(app.globalData.LastResult[cartid].num>1){
      app.globalData.LastResult[cartid].num = app.globalData.LastResult[cartid].num-1
      app.globalData.LastResult[cartid].allPrice = app.globalData.LastResult[cartid].allPrice - oneliaoprice
    }
    // console.log(cartid)
    this.setData({
      result:app.globalData.LastResult
    })
  },
  bindPlus:function(e){
    var cartid = e.currentTarget.dataset.addkey;
    var oneliaoprice = app.globalData.LastResult[cartid].allPrice / app.globalData.LastResult[cartid].num;
    app.globalData.LastResult[cartid].num = app.globalData.LastResult[cartid].num + 1
    app.globalData.LastResult[cartid].allPrice = app.globalData.LastResult[cartid].allPrice + oneliaoprice
    this.setData({
      result:app.globalData.LastResult
    })
  },
})
